﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Core.Models;
using System.Collections;

namespace Vowei.Converters
{
    internal class EmployeeComparer : IComparer<Employee>, IComparer
    {
        private Func<Employee, string> _compareFunc;

        public EmployeeComparer(Func<Employee, string> func)
        {
            _compareFunc = func;
        }

        public int Compare(Employee x, Employee y)
        {
            if (x == null && y == null)
                return 0;
            else if (x != null && y == null)
                return 1;
            else if (x == null && y != null)
                return -1;
            else
                return string.Compare(_compareFunc(x), _compareFunc(y), true);
        }

        public int Compare(object x, object y)
        {
            if (x == null && y == null)
                return 0;
            else if (x != null && y == null)
                return 1;
            else if (x == null && y != null)
                return -1;
            else if (x is Employee && y is Employee)
                return Compare((Employee)x, (Employee)y);
            else if (x is Employee && y is string)
                return string.Compare(_compareFunc((Employee)x), (string)y, true);
            else if (x is Employee && y is string)
                return string.Compare((string)x, _compareFunc((Employee)y), true);
            else if (x is string && y is string)
                return string.Compare((string)x, (string)y, true);
            else
                throw new NotSupportedException(string.Format("不支持对类型{0}和{1}作比较！", x.GetType(), y.GetType()));
        }
    }
}
